package com.xjoyt.pay.master.dao.predicate;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;

import com.xjoyt.pay.annotations.PredicateCommand;
import com.xjoyt.pay.master.params.MerchantCashPageParam;
import com.xjoyt.pay.master.po.Cash;

public class CashCommands {

	@PredicateCommand
	public List<Predicate> queryCommand(MerchantCashPageParam param, Root<Cash> root, CriteriaQuery<?> query,
			CriteriaBuilder cb) {

		if (StringUtils.isBlank(param.query))
			return null;
		List<Predicate> list = new ArrayList<>();
		for (String string : param.query.split(" ")) {
			if (StringUtils.isBlank(string))
				continue;
			String like = "%" + string + "%";
			list.add(cb.or(cb.like(root.get("merchantNo"), like), cb.like(root.get("merchantName"), like)));
		}
		return (List<Predicate>) list;
	}

	@PredicateCommand
	public List<Predicate> addedDateCommand(MerchantCashPageParam param, Root<Cash> root, CriteriaQuery<?> query,
			CriteriaBuilder cb) {

		List<Predicate> list = new ArrayList<>();
		if (param.startDate == null || param.endDate == null)
			return null;
		list.add(cb.between(root.get("addedDate"), param.startDate, param.endDate));
		return (List<Predicate>) list;
	}

}
